package com.xxxx.server.controller;


import com.xxxx.server.pojo.Admin;
import com.xxxx.server.pojo.RespBean;
import com.xxxx.server.pojo.Role;
import com.xxxx.server.service.IAdminService;
import com.xxxx.server.service.IRoleService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhoubin
 * @since 2020-10-15
 */
@RestController
public class AdminController {

    @Resource
    private IAdminService adminService;
    @Resource
    private IRoleService roleService;

    @GetMapping("system/admin")
    @ApiOperation("根据姓名模糊查询")
   public List<Admin> getAdminList(String keywords){

        return adminService.getAdminsByNameLike(keywords);
    }

    @GetMapping("/system/admin/roles")
    @ApiOperation("获得所有角色列表")
    public List<Role> getAllRoles(){
        return roleService.getRolesAll();
    }

    @ApiOperation("修改操作员角色")
    @PutMapping("/system/admin/role")
    public RespBean updateRoles(Integer adminId,String rids){
        System.out.println("rids:"+rids);
        return adminService.updateRolesById(adminId,rids);
    }


    @ApiOperation("修改启用状态")
    @PutMapping("/system/admin/")
    public RespBean updateEnabled(@RequestBody Map<String,Object> admin){
        boolean enabled = (boolean) admin.get("enabled");
        Integer adminId = (Integer) admin.get("id");
        //若enabled为true
        if (enabled){
           return adminService.updateEnableStatus(1, adminId);
        }else {
          return    adminService.updateEnableStatus(0, adminId);
        }

    }

    @ApiOperation(value = "修改个人信息")
    @PutMapping("/admin/info")
    public RespBean updateAdminInfo(@RequestBody Map<String,Object> admin){
        return  adminService.updateAdminInfo(admin);
    }

    @ApiOperation(value = "删除操作员")
    @DeleteMapping("/system/admin/{id}")
    public RespBean deleteAdminById(@PathVariable Integer id){

        return adminService.deleteAdminById(id);
    }

    @ApiOperation(value = "修改密码")
    @PutMapping("/admin/pass")
    public RespBean updatePassword(@RequestBody Map<String, Object> ruleForm){

        return adminService.updatePwdById(ruleForm);
    }

    @ApiOperation("修改头像")
    @RequestMapping("/admin/userface")
    public RespBean changeUserFace(MultipartFile file,Integer id){

        return adminService.changeUserFace(file,id);
    }


}
